package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;
import Models.InventoryUsage;

public class InventoryUsagePresenter extends AbstractPresenter<InventoryUsage> {
	final String SINGLEITEM = "inventoryUsage";
	final String MULTIITEM = "inventoryUsages";
	
	public ArrayList<InventoryUsage> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public InventoryUsage tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public InventoryUsage createItemFromJSONObject(JSONObject jsonObject){
		InventoryUsage item = new InventoryUsage();
		try{
			item.setAmount(jsonObject.getDouble("amount"));
			item.setId(jsonObject.getInt("invUsageID"));
			item.setUnits(jsonObject.getString("units"));
			
			POSSupplyViewPresenter presenter = new POSSupplyViewPresenter();
			JSONObject jsonGroup = jsonObject.getJSONObject("supplyID");
			item.setSupply(presenter.createItemFromJSONObject(jsonGroup));
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(InventoryUsage item){
		try{
			JSONObject json = new JSONObject();
			json.put("amount", item.getAmount());
			json.put("units", item.getUnits());
			json.put("invUsageID", item.getId());

			POSSupplyViewPresenter presenter = new POSSupplyViewPresenter();
			json.put("supplyID", presenter.createJSON(item.getSupply()));
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public InventoryUsage getEmptyEntity(){
		InventoryUsage entity = new InventoryUsage();
		return entity;
	}
}
